package com.nanhang.controller;

import com.nanhang.entity.Student;
import com.nanhang.service.StudentService;
import com.nanhang.util.PageResult;
import com.nanhang.util.R;
import com.nanhang.studentsearch.StudentS0;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping("/list")
    public List<Student> list(){
        return studentService.findAll();
    }

    @GetMapping("/findByPage")
    public PageResult<Student> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return studentService.findByPage(page,pageSize);
    }

    @PostMapping("/search")
    public PageResult<Student> search(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestBody(required = false) StudentS0 studentS0){
        return studentService.search(page,pageSize,studentS0);
    }

    @PostMapping("/save")
    public R save(@RequestBody Student student){
        return studentService.save(student) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }

    @PostMapping("/delete")
    public Integer deletes(@RequestParam("sid") Integer sid){
        return studentService.deletes(sid) ? 1:0;
    }
}
